/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.chassis.ems;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;

import twaver.Grid;
import twaver.TWaverConst;
import twaver.web.WebUtil;
import twaver.web.svg.network.SVGContext;
import twaver.web.svg.network.SVGStruct;
import twaver.web.svg.network.TSVGNetwork;
import twaver.web.svg.network.ui.GridSVGUI;

public class EMSSVGGridUI extends GridSVGUI {

	public EMSSVGGridUI(TSVGNetwork network, Grid grid) {
		super(network, grid);
	}

	protected SVGStruct toDefaultBodySVG(SVGContext context) {
		SVGStruct struct = super.toDefaultBodySVG(context);

		StringBuffer result = new StringBuffer();
		Rectangle rect = element.getBounds();
		Object percentObj = element.getClientProperty(EMSGridUI.PERCENT);
		double percent = 0;
		if (percentObj != null) {
			percent = Double.parseDouble(percentObj.toString());
		}
		WebUtil.appendShapeDirect(context, result, rect, Color.DARK_GRAY, null, null, 1f);
		rect.grow(-2, -2);
		double x = rect.getX();
		double y = rect.getY();
		double height = rect.getHeight();
		double width = rect.getWidth();

		WebUtil.appendShapeDirect(context, result, rect, null, Color.WHITE, TWaverConst.BASIC_STROKE, 0f);
		Rectangle2D.Double percentRect = new Rectangle2D.Double(x + 2, y + height * (1 - percent) + 0.5, width - 3, height * percent);
		Color color = Color.GREEN;
		if (percent > 0.6) {
			color = Color.ORANGE;
		}
		if (percent > 0.8) {
			color = Color.RED;
		}
		int factory = Integer.parseInt(element.getClientProperty(EMSGridUI.GRADIENTTYPE).toString());
		Object paint = getGradientID(context, factory, Color.WHITE, color);
		WebUtil.appendShapeDirect(context, result, percentRect, paint, null, null, 1f);

		struct.setSvg(result.toString());

		return struct;
	}
}